var webpack = require('webpack');
var webpackDevServer = require('webpack-dev-server');
var htmlWebpackPlugin = require('html-webpack-plugin'); //html打包
var uglifyJSPlugin = require('uglifyjs-webpack-plugin'); //引入js压缩插件
var extractTextPlugin = require('extract-text-webpack-plugin'); //分离css
var website = {
    publicPath: 'http://localhost:8000/'
}

module.exports = {
    entry: {
        main: './src/main.js',
        vendor: [
            'react',
            'react-dom'
        ]
    },
    output: {
        path: __dirname + '/dist',
        filename: '[name].[hash].js',
        publicPath: website.publicPath
    },
    devtool: 'cheap-eval-source-map',
    devServer: {
        port: 8000,
        contentBase: __dirname + '/dist',
        historyApiFallback: true
    },
    module: {
        rules: [
            {
                test: /\.js|.jsx?$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.less$/,
                use: extractTextPlugin.extract({
                    use: [
                        {loader: 'css-loader'},
                        {loader: 'less-loader'}
                    ],
                    fallback: 'style-loader'
                }),
            },
            {
                test: /\.(gif|png|jpg|woff|svg|ttf|eot)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 3000,
                            outputPath: 'images/',
                            name: '[name].[ext]?[hash]'
                        }
                    }
                ]
            },
            {
                test: /\.(htm|html)$/i,
                use: ['html-withimg-loader']
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
            Component: __dirname + '/src/Component',
            Routers: __dirname + '/src/Routers',
            Images: __dirname + '/src/Images'
        }
    },
    plugins: [
        new uglifyJSPlugin(), //压缩js代码
        new webpack.HotModuleReplacementPlugin(), //热加载
        new extractTextPlugin('css/h5.css'),
        new htmlWebpackPlugin({
            title: '主页',
            filename: 'index.html',
            template: __dirname + '/src/index.html'
        })
        // ,
        // new webpack.optimize.CommonsChunkPlugin({
        //     name: ['vendor', 'manifest'],
        //     minChunks: Infinity,
        //     minSize: 1024
        // })
    ]
}